package com.szcinda.controller;

import com.szcinda.controller.RSAUtil.RSAUtils;
import com.szcinda.service.CalculateService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"home"})
public class HomeController {


    private final CalculateService calculateService;

    public HomeController(CalculateService calculateService) {
        this.calculateService = calculateService;
    }

    @RequestMapping("/hello")
    public String hello() {
        return LocalDateTime.now().toString();
    }

    @RequestMapping("testRSA/{packageNo}")
    public Result<String> testRSA(@RequestHeader Map<String, String> headers, @PathVariable String packageNo) throws Exception {
        // 验证
        String signStr = headers.get("sign");
        if (StringUtils.isEmpty(signStr)) {
            return Result.fail("验签错误");
        }
        boolean result = RSAUtils.verify(packageNo.getBytes(), Base64.decodeBase64(signStr.getBytes()), RSAUtils.publicKey);
        return result ? Result.success() : Result.fail("验签错误");
    }


    // 今日订单数量和总金额
    @GetMapping("todayOrderCountAndMoney/{userId}/{orgId}")
    public CalculateService.DataDto todayOrderCountAndMoney(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.todayOrderCountAndMoney(userId, orgId);
    }

    // 当月订单数量和总金额
    @GetMapping("thisMonthOrderCountAndMoney/{userId}/{orgId}")
    public CalculateService.DataDto thisMonthOrderCountAndMoney(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.thisMonthOrderCountAndMoney(userId, orgId);
    }

    //
    @GetMapping("countByBusinessType")
    public List<CalculateService.DataDto> countByBusinessType() {
        return calculateService.countByBusinessType();
    }

    // 上月订单数量和总金额
    @GetMapping("lastMonthOrderCountAndAmount/{userId}/{orgId}")
    public List<CalculateService.DataDto> lastMonthOrderCountAndAmount(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.lastMonthOrderCountAndAmount(userId, orgId);
    }


    @GetMapping("countByOriginPlace")
    public List<CalculateService.DataDto> countByOriginPlace() {
        return calculateService.countByOriginPlace();
    }

    @GetMapping("countByName")
    public List<CalculateService.DataDto> countByName() {
        return calculateService.countByName();
    }

    @GetMapping("getOrderExpiredList/{userId}/{orgId}")
    public List<String> getOrderExpiredList(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.getOrderExpiredList(userId, orgId);
    }

    // 集装箱过期列表
    @GetMapping("getContainerOrderExpiredList/{userId}/{orgId}")
    public List<String> getContainerOrderExpiredList(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.getContainerOrderExpiredList(userId, orgId);
    }

    // 未完成订单号
    @GetMapping("getOrderNoCompletedList/{userId}/{orgId}")
    public List<String> getOrderNoCompletedList(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.getOrderNoCompletedList(userId, orgId);
    }

    // 合同过期列表
    @GetMapping("getContractExpireList/{userId}/{orgId}")
    public List<String> getContractExpireList(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.getContractExpireList(userId, orgId);
    }

    // 获取代办列表
    @GetMapping("daiBanShiXiangData/{userId}/{orgId}")
    public List<String> daiBanShiXiangData(@PathVariable String userId, @PathVariable String orgId) {
        return calculateService.daiBanShiXiangData(userId, orgId);
    }
}
